package com.kaiyun.sharding;


import com.baomidou.mybatisplus.core.parser.ISqlParser;
import com.baomidou.mybatisplus.extension.parsers.BlockAttackSqlParser;
import com.baomidou.mybatisplus.extension.plugins.PaginationInterceptor;
import lombok.extern.slf4j.Slf4j;
import org.mybatis.spring.annotation.MapperScan;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.context.ConfigurableApplicationContext;
import org.springframework.context.annotation.Bean;
import org.springframework.core.env.Environment;
import org.springframework.transaction.annotation.EnableTransactionManagement;

import java.net.InetAddress;
import java.net.UnknownHostException;
import java.util.ArrayList;
import java.util.List;


@SpringBootApplication
@MapperScan("com.kaiyun.sharding.mapper")
@Slf4j
@EnableTransactionManagement
public class ShardingApplication
{
    public static void main(String[] args) throws UnknownHostException {
        ConfigurableApplicationContext application = SpringApplication.run(
            ShardingApplication.class, args);
        Environment env = application.getEnvironment();
        String ip = InetAddress.getLocalHost().getHostAddress();
        String port = env.getProperty("server.port");
        String path = env.getProperty(
            "server.servlet.context-path") == null ? "" : env.getProperty(
            "server.servlet.context-path");
        log.info("\n----------------------------------------------------------\n\t"
            + "payServer is running! Access URLs:\n\t" + "Local: \t\thttp://localhost:"
            + port + path + "/\n\t" + "External: \thttp://" + ip + ":" + port + path + "/\n\t"
            + "swagger-ui: \thttp://" + ip + ":" + port + path + "/swagger-ui.html\n\t"
            + "Doc: \t\thttp://" + ip + ":" + port + path + "/doc.html\n"
            + "----------------------------------------------------------");


    }

    /**
     * mybatis-plus分页插件
     */
    @Bean
    public PaginationInterceptor paginationInterceptor() {
        PaginationInterceptor paginationInterceptor = new PaginationInterceptor();
        List<ISqlParser> sqlParserList = new ArrayList<>();
        // 攻击 SQL 阻断解析器、加入解析链
        sqlParserList.add(new BlockAttackSqlParser());
        paginationInterceptor.setSqlParserList(sqlParserList);
        return paginationInterceptor;
    }

}
